package cn.ffcs.uec.outStaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffMainBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostB;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostH;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffWork;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecOutStaffMainComposer extends BasePortletComposer implements IPortletInfoProvider {

    private static final long serialVersionUID = -5929030282722775845L;

    private UecOutStaffMainBean bean = new UecOutStaffMainBean();

    private UecOutStaffInfo uecOutStaffInfo;

    private UecOutStaffWork uecOutStaffWork;

    private UecOutStaffCostB uecOutStaffCostB;

    private UecOutStaffCostH uecOutStaffCostH;

    private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
            .getBean("uecOutStaffManager");

    /**
     * 操作类型.
     */
    private String opType = null; // 操作类型

    /**
     * 关闭窗口标志
     */
    private Boolean onCloseFlag;

    @Override
    public String getPortletId() {
        return super.getPortletId();
    }

    @Override
    public ThemeDisplay getThemeDisplay() {
        return super.getThemeDisplay();
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
        Components.wireVariables(comp, bean);
        bean.getUecOutStaffInfoListboxExt().setPortletInfoProvider(this);
        bean.getUecOutStaffInfoListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_INFO_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY_RESPONSE);
        bean.getUecOutStaffInfoListboxExt().addForward("onCloseOutStaffMainWindown", comp,
                "onCloseOutStaffMainWindownResponse");
        bean.getUecOutStaffInfoListboxExt().addForward(UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY, comp,
                UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY_RESPONSE);
        bean.getUecOutStaffCostBListboxExt().addForward("onSelectUecOutStaffCostB", comp,
                "onSelectUecOutStaffCostBRes");
        bean.getUecOutStaffCostHListboxExt().addForward("onSelectCostH", comp, "onSelectUecOutStaffCostHRes");
        // 外包人员试用情况
        bean.getUecOutStaffTrialListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY_RESPONSE);
        // 外包人员奖励信息
        bean.getUecOutStaffEncListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_RESPONSE);
        // 专业人才队伍信息
        bean.getUecOutStaffEncListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TALENTTEAM_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_TALENTTEAM_QUERY_RESPONSE);
        // 处罚信息
        bean.getUecOutStaffPunishListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY_RESPONSE);
        // 任职信息
        bean.getUecOutStaffPsnjobListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY_RESPONSE);
        // 考核信息
        bean.getUecOutStaffAssListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_ASS_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_ASS_QUERY_RESPONSE);
        // 政治面貌
        bean.getUecOutStaffPartyLogListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY_RESPONSE);
        // 离退情况
        bean.getUecOutStaffRetireListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY_RESPONSE);
        // 组织信息
        bean.getUecOutStaffPsnorgListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY_RESPONSE);
        // 人员家庭信息
        bean.getUecOutStaffFamilyListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY_RESPONSE);
        // 专业技能
        bean.getUecOutStaffTitleListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY_RESPONSE);
        // 教育培训
        bean.getUecOutStaffTrainListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY, comp,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY_RESPONSE);
        // 职业资格
        bean.getUecOutStaffNationdutyListboxExt().addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY,
                comp, UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY_RESPONSE);
    }

    /**
     * 界面初始化
     * 
     * @throws Exception
     */
    public void onCreate$uecOutStaffMainWin() throws Exception {
        bindBean();
    }

    /**
     * 
     * @throws Exception
     */
    public void bindBean() throws Exception {
        opType = StrUtil.strnull(arg.get("opType"));
        uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
        if ("view".equals(opType)) {
            if (uecOutStaffInfo != null) {
                Events.postEvent("onSelectUecOutStaffInfoByGetOrgTree", this.bean.getUecOutStaffInfoListboxExt(),
                        uecOutStaffInfo);
            }
        }
    }

    /**
     * 查询到外包员工基本信息的响应处理.
     * 
     * @param event
     */
    public void onUecOutStaffWorkQueryResponse(final ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (uecOutStaffInfo != null) {
            callTab();
        }
    }

    /**
     * 监听关闭窗口事件
     * 
     * @param event
     */
    public void onCloseOutStaffMainWindownResponse(final ForwardEvent event) {
        onCloseFlag = (Boolean) event.getOrigin().getData();
        if (onCloseFlag) {
            bean.getUecOutStaffMainWin().onClose();
        }
    }

    /**
     * 查询到外包员费用子表的响应处理.
     * 
     * @param event
     */
    public void onSelectUecOutStaffCostBRes(final ForwardEvent event) {
        uecOutStaffCostB = (UecOutStaffCostB) event.getOrigin().getData();
        /*
         * if(uecOutStaffCostB != null){ callTab(); }
         */
    }

    /**
     * 查询到外包员费用主表的响应处理.
     * 
     * @param event
     */
    public void onSelectUecOutStaffCostHRes(final ForwardEvent event) {
        uecOutStaffCostH = (UecOutStaffCostH) event.getOrigin().getData();
        /*
         * if(uecOutStaffCostH != null){ callTab(); }
         */
    }

    /**
     * 切换tab
     * 
     * @param event
     */
    public void onClickTab(ForwardEvent event) {
        try {
            Event origin = event.getOrigin();
            if (origin != null) {
                Component comp = origin.getTarget();
                if (comp != null && comp instanceof Tab) {
                    bean.setTab((Tab) comp);
                    callTab();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * tab响应
     * 
     * @throws Exception
     * 
     */
    public void callTab() {
        try {
            if (bean.getTab() == null) {
                bean.setTab(bean.getTabBox().getSelectedTab());
            }
            if (uecOutStaffInfo != null) {
                String tab = this.bean.getTab().getId();
                if ("uecOutStaffWork".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY,
                            this.bean.getUecOutStaffWorkListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffCostB".equals(tab)) {
                    uecOutStaffWork = uecOutStaffManager.findUecOutStaffWork(uecOutStaffInfo);
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTB_QUERY,
                            this.bean.getUecOutStaffCostBListboxExt(), uecOutStaffWork);
                } else if ("uecOutStaffCostH".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTH_QUERY,
                            this.bean.getUecOutStaffCostHListboxExt(), uecOutStaffCostB);
                } else if ("uecOutStaffManage".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_MANAGE_QUERY,
                            this.bean.getUecOutStaffManageListboxExt(), uecOutStaffCostH);
                } else if ("uecOutStaffVocqua".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_VOCQUA_QUERY,
                            this.bean.getUecOutStaffVocquaListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTramsg".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAMSG_QUERY,
                            this.bean.getUecOutStaffTramsgListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffContract".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_CONTRACT_QUERY,
                            this.bean.getUecOutStaffContractListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffDocEdu".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_DOC_EDU_QUERY,
                            this.bean.getUecOutDocEduListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTrial".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY,
                            this.bean.getUecOutStaffTrialListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffEnc".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY,
                            this.bean.getUecOutStaffEncListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTalentTeam".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TALENTTEAM_QUERY,
                            this.bean.getUecOutStaffTalentTeamListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPunish".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY,
                            this.bean.getUecOutStaffPunishListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPsnjob".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY,
                            this.bean.getUecOutStaffPsnjobListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffAss".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_ASS_QUERY,
                            this.bean.getUecOutStaffAssListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPartyLog".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY,
                            this.bean.getUecOutStaffPartyLogListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffRetire".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY,
                            this.bean.getUecOutStaffRetireListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPsnorg".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY,
                            this.bean.getUecOutStaffPsnorgListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffFamily".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY,
                            this.bean.getUecOutStaffFamilyListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffNationduty".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY,
                            this.bean.getUecOutStaffNationdutyListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTitle".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY,
                            this.bean.getUecOutStaffTitleListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTrain".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY,
                            this.bean.getUecOutStaffTrainListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffEnc".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY,
                            this.bean.getUecOutStaffEncListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffTalentTeam".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TALENTTEAM_QUERY,
                            this.bean.getUecOutStaffTalentTeamListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPunish".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PUNISH_QUERY,
                            this.bean.getUecOutStaffPunishListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffPsnWadoc".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNDOC_WADOC_QUERY,
                            this.bean.getUecOutPsndocWadocListboxExt(), uecOutStaffInfo);
                } else if ("uecOutStaffHiPsnWork".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUTSTAFF_HI_PSNWORK_QUERY,
                            this.bean.getUecOutStaffHiPsnWorkListboxExt(), uecOutStaffInfo);
                }
            } else {
                // 清空数据
                bean.getUecOutStaffWorkListboxExt().onCleanUecOutWorkResponse(null);
                bean.getUecOutStaffCostBListboxExt().onCleanUecOutCostBResponse(null);
                bean.getUecOutStaffCostHListboxExt().onCleanUecOutCostHResponse(null);
                bean.getUecOutStaffVocquaListboxExt().onCleanUecOutVocquaResponse(null);
                bean.getUecOutStaffTramsgListboxExt().onCleanUecOutTramsgResponse(null);
                bean.getUecOutStaffContractListboxExt().onCleanUecOutContractResponse(null);
                bean.getUecOutDocEduListboxExt().onCleanUecOutDocEduResponse(null);
                bean.getUecOutStaffTrialListboxExt().onCleanUecOutStaffTrialResponse(null);
                bean.getUecOutStaffEncListboxExt().onCleanUecOutStaffEncResponse(null);
                bean.getUecOutStaffTalentTeamListboxExt().onUecOutStaffTalentTeamQueryResponse(null);
                bean.getUecOutStaffPunishListboxExt().onUecOutStaffPunishQueryResponse(null);
                bean.getUecOutStaffPsnjobListboxExt().onUecOutStaffPsnjobQueryResponse(null);
                bean.getUecOutStaffAssListboxExt().onUecOutStaffAssQueryResponse(null);
                bean.getUecOutStaffPartyLogListboxExt().onUecOutStaffPartyLogQueryResponse(null);
                bean.getUecOutStaffPsnorgListboxExt().onUecOutStaffPsnorgQueryResponse(null);
                bean.getUecOutStaffRetireListboxExt().onUecOutStaffRetireQueryResponse(null);
                bean.getUecOutStaffFamilyListboxExt().onUecOutStaffFamilyQueryResponse(null);
                bean.getUecOutStaffNationdutyListboxExt().onUecOutStaffNationdutyQueryResponse(null);
                bean.getUecOutStaffTitleListboxExt().onUecOutStaffTitleQueryResponse(null);
                bean.getUecOutStaffTrainListboxExt().onUecOutStaffTrainQueryResponse(null);
                bean.getUecOutPsndocWadocListboxExt().onUecOutPsndocWodocQueryResponse(null);
                bean.getUecOutStaffHiPsnWorkListboxExt().onUecOutStaffHiPsnWorkQueryResponse(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理点击后Tabs .
     * 
     * @param event
     */
    public void onCleanTabUecStaffInfoQueryResponse(final ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (uecOutStaffInfo == null) {
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_WORK, bean.getUecOutStaffWorkListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTB, bean.getUecOutStaffCostBListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTH, bean.getUecOutStaffCostHListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_VOCQUA, bean.getUecOutStaffVocquaListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRAMSG, bean.getUecOutStaffTramsgListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_CONTRACT,
                    bean.getUecOutStaffContractListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRIAL, bean.getUecOutStaffTrialListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ENC, bean.getUecOutStaffEncListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TALENTTEAM,
                    bean.getUecOutStaffTalentTeamListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PUNISH, bean.getUecOutStaffPunishListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNJOB, bean.getUecOutStaffPsnjobListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ASS, bean.getUecOutStaffAssListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PARTYLOG,
                    bean.getUecOutStaffPartyLogListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNORG, bean.getUecOutStaffPsnorgListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_RETIRE, bean.getUecOutStaffRetireListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_FAMILY, bean.getUecOutStaffFamilyListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_NATION_DUTY,
                    bean.getUecOutStaffNationdutyListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_RETIRE, bean.getUecOutStaffRetireListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TITLE, bean.getUecOutStaffTitleListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_DOC_EDU, bean.getUecOutStaffContractListboxExt(),
                    null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNWADOC,
                    bean.getUecOutPsndocWadocListboxExt(), null);
            Events.postEvent(UecOutStaffConstants.ON_CLEAN_OUTSTAFF_HI_PSNWORK,
                    bean.getUecOutStaffHiPsnWorkListboxExt(), null);
            callTab();
        }
    }
}
